import {TodoDataSetActionTyep} from '../actions/todoActions.ts'
import {ActionAll} from '../entity/index.ts'
interface Todo {
  id:number,
  text:string,
  completed:boolean
}
const todos = [
  {
    id:0,
    text:'1111',
    completed:false
  },
  {
    id:1,
    text:'ssx',
    completed:false
  },
  {
    id:2,
    text:'1ddd111',
    completed:false
  },
  {
    id:3,
    text:'11ddd11',
    completed:false
  }
]
//初始值
const initialState:{
  todos:Todo[]
} = {
  todos
}

const todoReducer = (state=initialState,action)=>{
  switch (action.type) {
    case TodoDataSetActionTyep.ADD_TODO:
      return {
        ...state,
        todos:[...state.todos,{
          id:Date.now(),
          text:action.payload.text,
          completed:false
        }]
      }
      case TodoDataSetActionTyep.TOGGLE_TODO:
      return{
        ...state,
        todos:state.todos.map((todo)=>todo.id === action.payload.id ? {...todo,completed:!todo.completed}:todo)
      }
  
    default:
      return state;
  }
}

export default todoReducer